package com.example.spider.proxy.provider.kuaidaili;

import cn.hutool.core.util.ReUtil;
import com.example.spider.proxy.core.ProxyInfo;
import com.example.spider.proxy.core.tester.ProxyTypeEnum;
import com.example.spider.proxy.provider.AbstractProxyResultHandler;
import com.example.spider.proxy.provider.IpProxySpiderTask;
import com.example.spider.starter.SpiderStarter;
import com.example.spider.task.SpiderTaskQueue;
import lombok.extern.slf4j.Slf4j;
import org.shoulder.core.converter.LocalDateTimeConverter;

import java.time.Duration;
import java.util.List;

/**
 * https://www.kuaidaili.com/free/inha/1/
 * 每次每ip 3s间隔，否则返回 403/503，单个ip爬超过100触发限流
 * 可用性 1%
 * 每小时更新一次
 *
 * @author lym
 */
@Slf4j
//@Component
public class KuaiDailiDetailResultHandler extends AbstractProxyResultHandler implements SpiderStarter {

    public KuaiDailiDetailResultHandler() {
        super("kuaidaili");
    }

    protected Duration defaultLimitRateDuration() {
        return Duration.ofSeconds(5);
    }

    @Override
    public void startSpiders() {
        for (int i = 0; i < 100; i++) {
            String url = "https://www.kuaidaili.com/free/inha/" + i;
            IpProxySpiderTask task = createTask(url);
            SpiderTaskQueue.putTask(task);
        }
    }

    /**
     * 每10分钟触发一次
     */
    @Override
    public String executeCron() {
        return "0 0/10 * * * ?";
    }

    /**
     * handle
     */
    @Override
    protected void doHandle(IpProxySpiderTask task, String html) {
        // 要解析的太多了，先截取期望部分，避免每次解析全部html
        String tableRegex = "<tbody>(.*?)</tbody>";
        List<String> contentStr = ReUtil.findAllGroup1(tableRegex, html);
        if (contentStr.isEmpty()) {
            // 结束了
            log.error("kuaiDailiDetail invalid. Task:{}", task);
            return;
        }

        String table = contentStr.get(0);

        String ipRegex = "<td data-title=\"IP\">(.*?)</td>";
        String portRegex = "<td data-title=\"PORT\">(.*?)</td>";
        String anonymityRegex = "<td data-title=\"匿名度\">(.*?)</td>";
        String typeRegex = "<td data-title=\"类型\">(.*?)</td>";
        String locationRegex = "<td data-title=\"位置\">(.*?)</td>";
        String responseTimeRegex = "<td data-title=\"响应速度\">(.*?)秒</td>";
        String lastVerificationTimeRegex = "<td data-title=\"最后验证时间\">(.*?)</td>";

        List<String> ipList = ReUtil.findAllGroup1(ipRegex, table);
        List<String> portList = ReUtil.findAllGroup1(portRegex, table);
        List<String> anonymityList = ReUtil.findAllGroup1(anonymityRegex, table);
        List<String> typeList = ReUtil.findAllGroup1(typeRegex, table);
        List<String> locationList = ReUtil.findAllGroup1(locationRegex, table);
        List<String> responseTimeList = ReUtil.findAllGroup1(responseTimeRegex, table);
        List<String> lastVerificationTimeList = ReUtil.findAllGroup1(lastVerificationTimeRegex, table);

        for (int i = 0; i < ipList.size(); i++) {
            ProxyInfo proxyInfo = ProxyInfo.builder()
                    .source(taskName)
                    .ip(ipList.get(i))
                    .port(Integer.parseInt(portList.get(i)))
                    .anonymity(anonymityList.get(i))
                    .proxyTypeEnum(ProxyTypeEnum.of(typeList.get(i)))
                    .type(ProxyTypeEnum.of(typeList.get(i)).getType())
                    .location(locationList.get(i))
                    .responseTime(ProxyInfo.parseSecondToMill(responseTimeList.get(i)))
                    .lastVerificationTime(LocalDateTimeConverter.INSTANCE.convert(lastVerificationTimeList.get(i)))
                    .build();

            addProxyToSourceRepository(proxyInfo);
        }
    }

}
